//
//  MainController.m
//  微看
//
//  Created by 吴明磊 on 15/8/16.
//  Copyright © 2015年 wuminglei. All rights reserved.
//

#import "MainController.h"
#import "ModuleListController.h"

@interface MainController ()
@property(nonatomic,strong)ModuleListController *moduleListVc;

@end

@implementation MainController

- (void)viewDidLoad {
    [super viewDidLoad];
 


    
    [self setViewStyle];

    /// 准备navBar
    [self prepareBarView];
    
    [self notificationCenter];
    
   

    
}

- (void)viewDidLayoutSubviews {
    
    [self prepareModuleList];

    
}

//MARK: 通知
/// 通知中心
- (void)notificationCenter {

      // 接收navBar改变
      [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:@"NavBarModuleListSwitchNotification" object:nil];
    
      // 接收collectionView滚动
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:@"contentScrollNotification" object:nil];
}
/// navBar通知
- (void)receiveNotification:(NSNotification *)notification {

    NSInteger index = [[notification.userInfo objectForKey:@"index"] longValue];
    
    switch (index) {
        case 0:
            [self setViewStyleWithColor:[UIColor colorWithRed:80/255.0 green:92/255.0 blue:102/255.0 alpha:1] andNavColor:[UIColor colorWithRed:69/255.0 green:82/255.0 blue:92/255.0 alpha:1]];
            break;
        case 1:
            
             [self setViewStyleWithColor:[UIColor colorWithRed:193/255.0 green:95/255.0 blue:108/255.0 alpha:1] andNavColor:[UIColor colorWithRed:190/255.0 green:86/255.0 blue:99/255.0 alpha:1]];
            break;
        case 2:
            [self setViewStyleWithColor:[UIColor colorWithRed:98/255.0 green:167/255.0 blue:184/255.0 alpha:1] andNavColor:[UIColor colorWithRed:90/255.0 green:162/255.0 blue:180/255.0 alpha:1]];
            break;
        case 3:
            [self setViewStyleWithColor:[UIColor colorWithRed:55/255.0 green:173/255.0 blue:155/255.0 alpha:1] andNavColor:[UIColor colorWithRed:51/255.0 green:169/255.0 blue:150/255.0 alpha:1]];
            break;
        case 4:
            [self setViewStyleWithColor:[UIColor colorWithRed:181/255.0 green:129/255.0 blue:163/255.0 alpha:1] andNavColor:[UIColor colorWithRed:177/255.0 green:121/255.0 blue:159/255.0 alpha:1]];
            break;
    }
    

}


/// 设置view样式
- (void)setViewStyle {

    
    [self setViewStyleWithColor:[UIColor colorWithRed:80/255.0 green:92/255.0 blue:102/255.0 alpha:1] andNavColor:[UIColor colorWithRed:69/255.0 green:82/255.0 blue:92/255.0 alpha:1]];
}
- (void)setViewStyleWithColor:(UIColor *)viewColor andNavColor:(UIColor *)navColor {

   [UIView animateWithDuration:0.7 animations:^{
       
       self.view.backgroundColor = viewColor;
       self.navBar.backgroundColor = navColor;
   }];
}

/// 隐藏系统导航栏
- (void)hiddenNavBar {

     self.navigationController.navigationBar.hidden = YES;
}
/// 准备导航bar
- (void)prepareBarView {

    [self.view addSubview:self.navBar];
    
    
    self.navBar.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.navBar makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).offset(20);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.offset(60);
    }];
    
    [self.navBar loadBar];
    
    [self hiddenNavBar];
}
/// 准备collectionView控制器
- (void)prepareModuleList {

    
    [self.view addSubview:self.moduleListVc.view];
    
    self.moduleListVc.view.translatesAutoresizingMaskIntoConstraints = NO;
    
    
    [self.moduleListVc.view makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.navBar.bottom);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
}

//MARK: 懒加载
- (NavBar *)navBar {

    if (!_navBar) {
        _navBar = [[NavBar alloc]init];
    }
    
    return _navBar;
}


- (ModuleListController *)moduleListVc {

    if (!_moduleListVc) {
        _moduleListVc = [[ModuleListController alloc]init];
    }
    return _moduleListVc;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
